﻿/*jslint  browser: true, white: true, plusplus: true */
/*global $: true */

$(function () {
    'use strict';

    $.ajax({
        url: 'Controladores/GetAlumnos.php',
        dataType: 'json'
    }).done(function (data) {
        var status = $('#myTable'),
            alumnos = $.map(data, function (value) {
                return value;
            });

        $('#query').autocomplete({
            lookup: alumnos,
            onSelect: function (suggestion) {
            var bandera = true;
            var table = document.getElementById('myTable');
            var rowLength = table.rows.length;

            for (var i = 0; i < rowLength; i += 1) {
                var row = table.rows[i],
                    cellLength = row.cells.length   
                for (var y = 0; y < cellLength; y += 1) {
                    var cell = row.cells[1];
                    if(y==0)
                    {
                        if(cell.innerHTML == suggestion)
                        {
                            bandera = false;
                            break;
                        }
                    }

                }
            }
                if(bandera == true){
                    var Matricula = suggestion;
                    var path= "Controladores/GetAlumno.php?Matricula="+Matricula;  
                    $.ajax({
                            'url' : path,'type' : 'GET',
                            'success' : function(data) {
                                    data = data.split(",")[0];
                                    status.append('<tr id = '+ Matricula +'> <td> '+data+' </td> <td>'+Matricula+'</td> <td><img src=images/deletIco.png onClick=removerRow("'+Matricula+'") /> </td> </tr>');
                            }
                        });  
                }
             $('#query').val("");
                
            }
        });
    });
});